package com.dubbo.CarBalance.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.CarBalance.AccountAccessoriesService;
import com.dubbo.CarBalance.AccountFacilitatorService;
import com.dubbo.CarBalance.AccountPayBillsService;
import com.dubbo.CarBalance.mapper.AccountAccessoriesMapper;
import com.dubbo.CarBalance.mapper.AccountDeatilMapper;
import com.dubbo.CarBalance.mapper.AccountPayBillsMapper;
import com.dubbo.CarBalance.utils.PageUtils;
import com.dubbo.common.dtos.AppHttpCodeEnum;
import com.dubbo.common.dtos.ResponseResult;
import com.dubbo.common.exception.CarServiceException;
import com.dubbo.model.balance.dtos.*;
import com.dubbo.model.balance.pojos.AccountAccessories;
import com.dubbo.model.balance.pojos.AccountDetail;
import com.dubbo.model.balance.pojos.AccountPayBills;
import com.dubbo.utils.common.BeanHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class AccountPayBillsServiceImpl implements AccountPayBillsService {

    @Autowired
    private AccountPayBillsMapper mapper;

    @Autowired
    private AccountAccessoriesService service;

    @Autowired
    private AccountFacilitatorService facilitatorService;

    /*@Autowired
    private AccountDeatilMapper deatilMapper;

    @Autowired
    private AccountAccessoriesMapper accessoriesMapper;*/

    @Override
    public ResponseResult unpaid(UnPayQueryDto dto) {
        if (dto==null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        return PageUtils.getPageResult(dto,getPageInfo(dto));
    }

    @Override
    public ResponseResult payQuery(payQueryDto dto) {
        if (dto==null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        return PageUtils.getPageResult(dto,getPageInfoPay(dto));
    }

    @Override
    public ResponseResult<AccountDetailDto> payDetail(String id) {
        if (id==null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }
        AccountPayBills accountPayBills = mapper.selectByPaymentId(id);
        String accountId = accountPayBills.getAccountId();

//        ResponseResult<AccountDetailDto> detail = detail(accountId);
        ResponseResult<AccountDetailDto> result = service.detail(accountId);
        if (result.getCode().equals(200)){
            return result;
        }

        return null;
    }

    @Override
    public ResponseResult<List<AccountAccessPayResultDto>> pay(AccountAccessoriesPayDto dto) {
        if (dto==null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getPayAcType().equals(0)) {
            ResponseResult<List<AccountAccessPayResultDto>> responseResult = service.account(dto);
            if (responseResult.getCode().equals(200)){
                return responseResult;
            }
        }else{
            ResponseResult<List<AccountAccessPayResultDto>> account = facilitatorService.account(dto);
            if (account.getCode().equals(200)){
                return account;
            }
        }
        return null;
    }

    private PageInfo<UnpaidDto> getPageInfo(UnPayQueryDto dto){
        int page = dto.getPage();
        int size = dto.getSize();
        PageHelper.startPage(page,size);
        List<AccountPayBills> accountPayBills = mapper.selectUnPay(dto);
        if (CollectionUtils.isNotEmpty(accountPayBills)){
            List<UnpaidDto> unpaidDtos = BeanHelper.copyWithCollection(accountPayBills, UnpaidDto.class);
            return new PageInfo<UnpaidDto>(unpaidDtos);
        }
        return new PageInfo<>();
    }

    private PageInfo<PaidDto> getPageInfoPay(payQueryDto dto){
        int page = dto.getPage();
        int size = dto.getSize();
        PageHelper.startPage(page,size);
        List<AccountPayBills> accountPayBills = mapper.selectPay(dto);
        if (CollectionUtils.isNotEmpty(accountPayBills)){
            List<PaidDto> paidDtos = BeanHelper.copyWithCollection(accountPayBills, PaidDto.class);
            return new PageInfo<PaidDto>(paidDtos);
        }
        return new PageInfo<>();
    }


    /*public ResponseResult<AccountDetailDto> detail(String id) {
        //参数校验
        if (id == null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }

        AccountDetailDto detailDto = new AccountDetailDto();

        //查询配件商账单明细
        AccountAccessories accessories = accessoriesMapper.findById(id);
        if (accessories!=null){
            detailDto.setAccountId(accessories.getAccountId());
            detailDto.setDigest(accessories.getDigest());
            detailDto.setPrice(accessories.getPrice());
            detailDto.setRemark(accessories.getRemark());
        }

        //查询配件商账单详情
        AccountDetail accountDetail = deatilMapper.findbyAccountId(id);
        if (accountDetail!=null){
            detailDto.setAccountDetail(accountDetail);
        }

        return ResponseResult.okResult(detailDto);
    }*/
}
